package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.example.dao.goods.GoodsMapper;
import org.example.entity.*;
import org.example.service.GoodsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;


    /**
     *新增用户
     **/

    @Override
    public void addGoods(Goods goods) {
        StringBuffer imageUrl = new StringBuffer(goods.getGoodsImage());
        imageUrl.replace(0,24,"");
        System.out.println(goods.getGoodsImage());
        goods.setGoodsImage(imageUrl.toString());
        System.out.println(goods.getGoodsImage());
        goodsMapper.addGoods(goods);
        for (int i = 0;i < goods.getSpecs().size();i++) {
            goods.getSpecs().get(i).setGoodsId(goods.getGoodsId());
            goodsMapper.addSpecs(goods.getSpecs().get(i));
        }

    }

    /**
     *按条件分页查询商品列表
     **/
    @Override
    public PageBean<Goods> listByReason(Integer pageSize,Integer page,
                                        String goodsName,String categoryName,String status) {
        if (status.equals("是")) {
            status = "1";
        }else if (status.equals("否"))
            status = "0";
        PageHelper.startPage(page,pageSize);
        List<Goods> list = goodsMapper.listByReason(goodsName,categoryName,status);

        Page<Goods> p = (Page<Goods>) list;

        long total = p.getTotal();
        list = p.getResult();
        for (int i = 0;i < list.size();i++) {
            Goods goods = list.get(i);
            list.get(i).setSpecs(goodsMapper.specsList(goods.getGoodsId()));
        }
        return new PageBean<Goods>(total,list);
    }

    /**
     * 删除商品及规格
     * */
    @Override
    public void delete(Integer goodsId) {
        goodsMapper.deleteGoods(goodsId);
        goodsMapper.deleteSpecs(goodsId);
    }
    /**
     * 更新商品
     * */
    @Override
    public void update(Goods goods) {

        for (int i = 0;i < goods.getSpecs().size();i++) {
            Specs specs = goods.getSpecs().get(i);
            if (specs.getSpecsId() != null)
                goodsMapper.updateSpecs(specs);
            else {
                specs.setGoodsId(goods.getGoodsId());
                goodsMapper.addSpecs(specs);
            }
        }
    }

    @Override
    public List<String> list() {
        return goodsMapper.list();
    }

    /**
     * 微信小程序查询全部商品
     * */

    @Override
    public List<Goods> goodsList() {
        List<Goods> list = goodsMapper.goodsList();
        for (int i = 0;i < list.size();i++) {
            Goods goods = list.get(i);
            list.get(i).setSpecs(goodsMapper.specsList(goods.getGoodsId()));
        }
        return list;
    }

    /**
     * 按分类查询商品
     * */
    @Override
    public List<GoodsByCategory> getGoodsByCategory(List<Category> list) {
        List<GoodsByCategory> goodsByCategory = new ArrayList<GoodsByCategory>();
        goodsByCategory.add(new GoodsByCategory(list.get(0),goodsMapper.getHot()));

        for (int i=1;i<list.size();i++){
            goodsByCategory.add(new GoodsByCategory(
                    list.get(i),goodsMapper.getGoodsByCategory(list.get(i).getCategoryName())));
        }

        //按照商品id赋予规格
        for (int i = 0;i < goodsByCategory.size();i++) {
            for (int j = 0;j < goodsByCategory.get(i).getGoodsList().size();j++) {
                goodsByCategory.get(i).getGoodsList().get(j).setSpecs(
                        goodsMapper.specsList(goodsByCategory.get(i).getGoodsList().get(j).getGoodsId()));
            }
        }
        return goodsByCategory;
    }
}
